# 这里假定 git 上项目名就是服务名，如果出现了例外，记得修改 git 项目名，而不要在这里修改。
# 仅当项目初始化还未上传 git 时可以临时的手动设置 PACKAGE_NAME 值，绕过各种检查。
SUB_PACKAGE  := $(subst $(shell git rev-parse --show-toplevel),,$(CURDIR))
PACKAGE_ROOT := $(shell git remote -v | grep '^origin\s.*(fetch)$$' | awk '{print $$2}' | sed -E 's/^.*(\/\/|@)(.*)\.git\/?$$/\2/' | sed 's/:/\//g')
PACKAGE_NAME = $(PACKAGE_ROOT)$(SUB_PACKAGE)

APP_ROOT := $(shell dirname $(PACKAGE_NAME))
APP      := $(shell basename $(PACKAGE_NAME))

OUTPUT     = $(CURDIR)/output
CONF       = $(CURDIR)/conf

TEMP_FILE := $(shell mktemp -u)_you_can_delete_this_file


OUTPUT_DIRS = conf bin plugins
TIME_STAMP=$(shell date "+%Y%m%d%H%M%S")
include ./Makefile.in

.DEFAULT: all
all: build-branch

build-linux: clean prepare fmt
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "$(OUTPUT)/bin/$(APP)" "$(CURDIR)/main.go"
build-branch:clean prepare fmt
	go build  -o "$(OUTPUT)/bin/$(APP)" "$(CURDIR)/main.go"
#build-master: clean prepare fmt
#	# 预先拉取master的最新分支作为代理依赖
#	go get -u
#	go build -o "$(OUTPUT)/bin/$(APP)" "$(CURDIR)/main.go"
fmt:
	go fmt ./...

clean:
	for i in $(OUTPUT_DIRS); do rm -rf "$(OUTPUT)/$$i"; done

prepare:
	mkdir -p "$(OUTPUT)/log"
	for i in $(OUTPUT_DIRS); do mkdir -p "$(OUTPUT)/$$i"; done
	cp -vr "$(CONF)" "$(OUTPUT)"
run:
	cd "$(OUTPUT)" && bin/$(APP)

test:
	go test ./...

init:
	go mod download

.PHONY: all build fmt clean prepare run test init
$(VERBOSE).SILENT:
